﻿/// <reference path="../../scripts/q.js" />

/*globals Q */

///
/// Fake for an an $-promise object
///
( function ( crk, undefined ) {
    'use strict';

    ///
    /// Retruns a promise that invokes the resolve method
    ///
    function buildFakeResolvePromise( resolveResult, opt_timeout ) {

        var dfd = Q.defer(),
            delay = ( opt_timeout === undefined ) ? 0 : opt_timeout;

        window.setTimeout( function () {

            dfd.resolve( resolveResult );

        }, delay );
        
        return dfd.promise;
    }


    ///
    /// Returns a very simple mock of an promise object
    ///
    function getSimplePromiseMock( resolveResult ) {

        return {
            then: function ( callback ) {

                callback.call( null, resolveResult );
            }
        };
    }


    ///
    /// Returns a promise that invokes the reject method
    ///
    function buildFakeRejectPromise( rejectResult, opt_timeout ) {

        var dfd = new Q.defer(),
            delay = ( opt_timeout === undefined ) ? 100 : 0;

        window.setTimeout( function () {

            dfd.reject( rejectResult );

        }, delay );


        return dfd.promise;
    }

    ///
    /// Return an already resolved promsie
    ///
    function buildResolvedPromise( resolveResult ) {

        var dfd = new Q.defer();

        dfd.resolve( resolveResult );

        return dfd.promise;
    }

    ///
    /// Public API
    ///
    crk.fake = crk.fake || {};
    crk.fake.promise = {
        buildFakeResolvePromise: buildFakeResolvePromise,
        buildFakeRejectPromise: buildFakeRejectPromise,
        buildResolvedPromise: buildResolvedPromise,
        getSimplePromiseMock: getSimplePromiseMock
    };
}( window.crk = window.crk || {} ) );